﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;

namespace DummyServerUI
{
    public partial class Form1 : Form
    {
        private StreamReader sr;
        private StreamWriter sw;

        public Form1()
        {
            InitializeComponent();
            TcpListener listener = new TcpListener(1987);
            listener.Start();
            TcpClient client = listener.AcceptTcpClient();
            sr = new StreamReader(client.GetStream());
            sw = new StreamWriter(client.GetStream());
            string message;
            BackgroundWorker receiving = new BackgroundWorker();
            receiving.DoWork += new DoWorkEventHandler(receiving_DoWork);
            receiving.WorkerReportsProgress = true;
            receiving.ProgressChanged += new ProgressChangedEventHandler(updateReceived);
            receiving.RunWorkerAsync();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            sw.WriteLine(textBox1.Text);
            sw.Flush();
        }
        private void receiving_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bg = sender as BackgroundWorker;
            string message;
            while (true)
            {
                try
                {
                    message = sr.ReadLine();
                    if (message != null)
                    {
                        bg.ReportProgress(1, message);
                    }
                }
                catch (Exception wtf)
                {
                    Console.WriteLine(wtf.Message);
                }
            }

        }
        private void updateReceived(object sender, ProgressChangedEventArgs e)
        {
            textBox2.AppendText( Environment.NewLine+ e.UserState.ToString());
        }
        
    }

}